use actix_web::{get, web, App, HttpServer, Responder};
use dotenv::dotenv;
use sqlx::{Done, MySqlPool};
use std::env;

#[get("/hello")]
async fn index(pool: web::Data<MySqlPool>) -> impl Responder {
    let result = sqlx::query!(
        r#"
UPDATE test_table
SET field_1 = 200
WHERE id = 0
        "#
    )
    .execute(pool.get_ref())
    .await
    .unwrap();

    format!("有效影响到的行数: {}", result.rows_affected())
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL").expect("没有从环境变量中获得数据库地址");

    let pool = MySqlPool::connect(&database_url).await.unwrap();

    HttpServer::new(move || App::new().data(pool.clone()).service(index))
        .bind("127.0.0.1:8080")?
        .run()
        .await?;

    Ok(())
}
